
#include "Objects/GameObject.h"

GameObject::GameObject(GameState *gameState) : m_gameState(gameState),
						   m_position(0.0f, 0.0f),
						   m_rotation(0.0f),
						   m_size(1.0f, 1.0f),
						   m_visible(true),
						   m_alive(true),
						   m_texture(NULL),
						   m_firstUpdate(true),
						   m_id(0),
						   m_layer(0)
{
	setColor(1.0f, 1.0f, 1.0f, 1.0f);
	m_screen = m_gameState->getCurrentScreen();
}


GameObject::~GameObject(){}


bool GameObject::hasProperty(const string &name) const{
	return m_properties.count(name);
}


void GameObject::addProperty(const string &name){
	m_properties.insert(name);
}


void GameObject::deleteProperty(const string &name){
	m_properties.erase(m_properties.find(name));
}


const b2Vec2& GameObject::getPosition() const{ 
	return m_position; 
}


void GameObject::setPosition(const b2Vec2 &position){ 
	m_position = position;
}


float GameObject::getRotation() const{ 
	return m_rotation; 
}


void GameObject::setRotation(float radians){
	m_rotation = radians;
}


const b2Vec2& GameObject::getSize() const{ 
	return m_size;
}


void GameObject::setSize(const b2Vec2 &size){
	m_size = size;
}


sf::Texture* GameObject::getTexture(){
	return m_texture;
}


void GameObject::setTexture(sf::Texture &texture){ 
	m_texture = &texture;
}


const float* GameObject::getColor() const{
	return m_color;
}


void GameObject::setColor(float r, float g, float b, float a){
	m_color[0] = r;
	m_color[1] = g;
	m_color[2] = b;
	m_color[3] = a;
}


void GameObject::draw(){
	if(!m_visible)
		return;

	if(m_texture)
		sf::Texture::bind(m_texture);
	else
		glColor4fv(m_color);

	b2Vec2 position = getPosition();
	float rotation = getRotation();
	b2Vec2 size = getSize();

	glPushMatrix();
	{
		glTranslatef(position.x,position.y,0);
		glRotatef(rotation,0,0.0f,1);
		glScalef(size.x, size.y, 0);
		glBegin(GL_QUADS);
		{
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, 0.0f);
		}
		glEnd();
	}
	glPopMatrix();

	sf::Texture::bind(NULL);
}


